package com.yuexun.beilunpatient.ui.bodyCheck.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.bean.BaseListEntity;
import com.yuexun.beilunpatient.ui.bodyCheck.Adapter.BasicAdapter;
import com.yuexun.beilunpatient.ui.bodyCheck.Adapter.HosAdpater;
import com.yuexun.beilunpatient.ui.bodyCheck.bean.BodyCheckBasicBean;
import com.yuexun.beilunpatient.ui.bodyCheck.bean.BodyCheckHosBean;
import com.yuexun.beilunpatient.ui.bodyCheck.model.impl.BodyCheckModel;
import com.yuexun.beilunpatient.ui.bodyCheck.presenter.impl.BasicPresenter;
import com.yuexun.beilunpatient.ui.bodyCheck.presenter.impl.HosPresenter;
import com.yuexun.beilunpatient.ui.bodyCheck.ui.view.IBasicView;
import com.yuexun.beilunpatient.ui.bodyCheck.ui.view.IHosView;
import com.yuexun.beilunpatient.ui.main.ui.Act_Webview;
import com.yuexun.beilunpatient.utils.IdNOToAgeUtil;
import com.yuexun.beilunpatient.widget.EmptyLayout;
import com.yuexun.beilunpatient.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.beilunpatient.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.beilunpatient.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;
import org.kymjs.kjframe.utils.SystemTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019/4/28 0028.
 */

public class Frag_Hos_New extends KJFragment implements IHosView {
    @Bind(R.id.content_list)
    PullToRefreshList pullList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private HosPresenter presenter;
    private int nowpage = 1;
    private int pagesize = 20;
    private ListView listView;
    HosAdpater adapter;
    ArrayList<BodyCheckHosBean.RowsBean> rowsBeans = new ArrayList<>();

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.frag_bodycheck, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initData() {
        presenter = new HosPresenter(this, new BodyCheckModel());

        initList();
        ApiGetData();
    }

    /**
     * 初始化列表
     */
    private void initList() {
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                nowpage = 1;
                ApiGetData();
            }
        });

        listView = pullList.getRefreshView();
        listView.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        pullList.setPullRefreshEnabled(true);
        pullList.setPullLoadEnabled(true);
        ((FooterLoadingLayout) pullList.getFooterLoadingLayout()).setNoMoreDataText("无更多数据");

        adapter = new HosAdpater(listView, rowsBeans, R.layout.item_bodycheck);
        listView.setAdapter(adapter);

        pullList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                nowpage = 1;
                ApiGetData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                nowpage = nowpage + 1;
                ApiGetData();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(), Act_Webview.class);
                intent.putExtra("url", AppConfig.WEB_H5+"hospExamInfo.html"
                        +"?sessionUuidPatient="+PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID)
                        +"&chkupno="+rowsBeans.get(i).getChkupno()
                        +"&personId="+rowsBeans.get(i).getPersonid()
                        +"&name="+PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.USER_NAME)
                        +"&sex="+PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SEX)
                        +"&card="+PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.ID_CARD)
                        +"&affiliate="+rowsBeans.get(i).getHospitalname()
                        +"&age="+ IdNOToAgeUtil.IdNOToAge(PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.ID_CARD)));
                intent.putExtra("title", rowsBeans.get(i).getExamtime()+"体检详情");
                startActivity(intent);
            }
        });
    }

    /**
     * 请请求数据
     */
    private void ApiGetData() {
        if (SystemTool.checkNet(getActivity())) {
            Map<String, String> params = new HashMap<>();
            params.put("sessionUuidPatient", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
            params.put("personId", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.ID_CARD));
            params.put("pageIndex", nowpage + "");
            params.put("pageSize", pagesize + "");
            presenter.inquireBodyTestList(params);
        } else {
            ViewInject.toast("网络异常，请检查网络是否连接!");
        }
    }

    @Override
    public void inquireBodyTestList(BaseListEntity<BodyCheckHosBean> entity) {
        if (pullList.isPullRefreshEnabled()) {
            pullList.onPullDownRefreshComplete();
        }
        if (entity.getStatus().equals("200")) {
            if (entity.getDatas().get(0).getCode() == 1) {
                if (entity.getDatas().get(0).getRows().size() == 0) {
                    pullList.setHasMoreData(false);
                } else {
                    if (pullList.isPullLoadEnabled()) {
                        pullList.onPullUpRefreshComplete();
                    }
                }

                if (nowpage == 1) {
                    rowsBeans.clear();
                    rowsBeans = entity.getDatas().get(0).getRows();
                } else {
                    rowsBeans.addAll(entity.getDatas().get(0).getRows());
                }
                adapter.refresh(rowsBeans);
            } else {
                ViewInject.toast(entity.getDatas().get(0).getDesc());
            }

        } else {
            ViewInject.toast(entity.getMessage());
        }

        if (rowsBeans.size() == 0) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            emptyLayout.dismiss();
        }
    }
}
